import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';

import 'action.dart';
import 'state.dart';

import 'package:flutter/gestures.dart';

import 'package:rss_readneed/punch_card/punchCardModel.dart';
import 'package:rss_readneed/public.dart';
import 'package:rss_readneed/sn_support/SNAccount.dart';
import 'package:rss_readneed/common/sliverAppbar_gradient.dart';

Widget buildView(simple_show_snState state, Dispatch dispatch, ViewService viewService) {

  Color colorWithTime (PunchCardModel model) {
    
    if (model.finishTime == null || model.finishTime.length == 0 || model.dateTime == null || model.dateTime.length == 0) {

      return Colors.black;
    }

    int difHour = DateTime.parse(model.finishTime).difference(DateTime.parse(model.dateTime + state.monthModel.finishTime.substring(10))).inHours;

    Color timeColor = Colors.black45;
    if (difHour <= 0) {//0-1准点下班

      timeColor = Colors.black45;
    } else if (difHour <= 2) {//2-3小时，还可以

      timeColor = Colors.green;
    } else if (difHour <= 4) {//4-5小时，黄线

      timeColor = Colors.yellow;
    } else if (difHour <= 5) {//5-6小时，红线

      timeColor = Colors.red;
    } else {//>6小时，死亡芭比粉~

      timeColor = Colors.pink;
    }
    return timeColor;
  }

  return CustomScrollView(
    controller: state.scrollController,
    slivers: <Widget>[

      GradientSliverAppBar(
        title:Opacity(opacity: state.alpha,child: Text(DateTime.now().toString().substring(0,7),style: TextStyle(fontSize: 24,color: Colors.white,),),),
        leading:IconButton(icon: Icon(Icons.transform), onPressed: (){ dispatch(simple_show_snActionCreator.changeFirstPage()); /*切换主页面*/}),
        actions:<Widget>[
          Container(child: SNAccount.instance.isFatch ?
          Container()
              : IconButton(icon: Icon(Icons.link), onPressed: (){ AppNavigator.pushRoute(viewService.context, "sn"); })//跳转sn登录
          )
        ],
        shadowColor: Theme.of(viewService.context).primaryColor,
        gradient: RadialGradient(//更改为圆扩散
            colors: [Theme.of(viewService.context).primaryColorLight,Theme.of(viewService.context).primaryColorDark],
            center: Alignment.topLeft,
            radius: 4),
        floating:true,//跟随滑动展示头视图
        pinned: true,//在页面上不消失
        expandedHeight: 100,//扩展高度
        flexibleSpace:FlexibleSpaceBar(//扩展bar
          title: Text.rich(TextSpan(
            text: "",
            children: [
//              TextSpan(
//                  text: "总时长  ",
//                  style: TextStyle(color: Colors.black38,fontSize: 12,fontWeight: FontWeight.w400)
//              ),
//              TextSpan(
//                  text: state.totalShowTime,
//                  style: TextStyle(color: Colors.blue,fontSize: 21,fontWeight: FontWeight.w400),
//              ),
              TextSpan(
                  text: "      平均下班  ",
                  style: TextStyle(color: Colors.black38,fontSize: 12,fontWeight: FontWeight.w200)
              ),
              TextSpan(
                  text: state.alreadyfinishTime.length > 16 ? state.alreadyfinishTime.substring(11,16) : "",
                  style: TextStyle(color: colorWithTime(PunchCardModel(dateTime: state.alreadyfinishTime.length > 16 ? state.alreadyfinishTime.substring(0,10) : "",finishTime:state.alreadyfinishTime)),fontSize: 21,fontWeight: FontWeight.w300),
              ),
            ]
          )),
          centerTitle: true,
//            title: Text("SubTitle Show".toUpperCase()),
//          background: Image.network(state.datas[0]["url"],fit: BoxFit.cover,),
        ),
//         //底部规划
//        bottom: PreferredSize(
//            child: Text.rich(TextSpan(
//                      text: "",
//                      children: [
//                        TextSpan(
//                            text: "月计划  ",
//                            style: TextStyle(color: Colors.black38,fontSize: 12,fontWeight: FontWeight.w200)
//                        ),
//                        TextSpan(
//                            text: state.monthPlanTime == null ? "选择时间" : state.monthPlanTime,
//                            style: TextStyle(color: Colors.blue,fontSize: 18,fontWeight: FontWeight.w400),
//                        ),
//                        TextSpan(
//                            text: "剩余计划  ",
//                            style:TextStyle(color: Colors.black38,fontSize: 14,fontWeight: FontWeight.w600),
//                            children: [TextSpan(
//                              text: state.leftDayPlanFinishTime,
//                              style: TextStyle(color: Colors.orange,fontSize: 24,fontWeight: FontWeight.w300),
//                            )]
//                        )
//            ])),
//            preferredSize: Size(double.infinity, 50)
//        ),//扩展底部按钮
      ),
      SliverSafeArea(
        top: false,//头部非安全
        minimum: EdgeInsets.all(4),
        sliver: SliverList(
            delegate: SliverChildListDelegate(
                state.datas.map((PunchCardModel model){

                  return PunchCardListCell(
                    model: model,
                    timeColor: colorWithTime(model),
                    checkCallBack: (){ dispatch(simple_show_snActionCreator.changeCheckAction(model));/*更改是否统计*/ },
//                    timeCallBack: (){ dispatch(punch_card_listActionCreator.changeTimeAction(model)); },
                  );
                }).toList(),
              semanticIndexCallback: (_,int) {
                  print(int);
              },

            )
        ),
      ),
    ],
  );
}

class PunchCardListCell extends StatefulWidget {

  final PunchCardModel model;
  final Color timeColor;
  final VoidCallback checkCallBack;
  final VoidCallback timeCallBack;

  PunchCardListCell({
    this.model,
    this.timeColor,
    this.checkCallBack,
    this.timeCallBack,
  });

  @override
  PunchCardListCellState createState() => new PunchCardListCellState();
}

class PunchCardListCellState extends State<PunchCardListCell> {

  TapGestureRecognizer _tapGestureRecognizer = new TapGestureRecognizer();

  String weekDesc (int weekday) {

    switch (weekday) {

      case 1:return "周一";
      case 2:return "周二";
      case 3:return "周三";
      case 4:return "周四";
      case 5:return "周五";
      case 6:return "周六";
      case 7:return "周日";
    }
    return "";
  }

  @override
  Widget build(BuildContext context) {

    int weekday = DateTime.parse(widget.model.dateTime).weekday;

    return ListTile(
      trailing: Checkbox(value: widget.model.inCount, onChanged: (incount){ widget.checkCallBack != null ? widget.checkCallBack() : (){};/*更改是否统计*/},),
      title: RichText(text: TextSpan(
        text: widget.model.dateTime.substring(5,10) + "  ",//加上间隔
        style: TextStyle(color: Colors.black,fontSize: 18,fontWeight: FontWeight.w600),
        children: [
          TextSpan(text:weekDesc(weekday) + " ",
            style: TextStyle(color: weekday < 6 ? Colors.black:Colors.black38,fontSize: 12,fontWeight: FontWeight.w400),
          ),
          TextSpan(text:(widget.model.finishTime.contains(widget.model.dateTime) ? widget.model.finishTime.substring(11,16) : widget.model.finishTime),
            style: TextStyle(color: widget.timeColor,fontSize: 24,fontWeight: FontWeight.w300),
            recognizer: _tapGestureRecognizer
              ..onTap = widget.timeCallBack != null ? widget.timeCallBack : (){},//判空处理，手势非判空报错
          )
        ],
      )),
      subtitle: Text(widget.model.workingThings),
//            onTap: (){ AppNavigator.push(ctx, punchCard.punch_cardPage().buildPage({"model":model}));},//删除跳转,更改为对时间添加点击事件切换时间
    );
  }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  void dispose() {
    _tapGestureRecognizer.dispose();
    super.dispose();
  }

  @override
  void didUpdateWidget(PunchCardListCell oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
  }
}